<?php

include_once dirname(__DIR__).'/third_party/phpcode_shield.php';		//加载防注入代码
class Manage extends CI_Controller 
{
	/*
		构造函数
	*/
	function __construct() {
		header('Content-Type: text/html; charset=utf-8'); 
		parent::__construct();
		$this->load->helper('url');
	}
	
	
	/*
		判断是否微信的
	*/
	public function is_weixin() { 
		if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
				return true;
		}

		return false;
	}


	private function getAccountIdByOpenid($open_id)
	{
		$request_ary['open_id'] = $open_id;
		$this->load->model('account/account_model','',true);
		$userinfo_result = $this->account_model->getUserInfo($request_ary);
		if(!isset($userinfo_result['data']['account_id']) || $userinfo_result['data']['account_id'] == "")
		{
			return -1;
		}
		$account_id = $userinfo_result['data']['account_id'];
		return $account_id;
	}
	
	/*
		提示在微信浏览器打开
	*/
	public function warmingWX() {
		$data['base_url'] = base_url();				
		$this->load->view("warming_wx",$data);
	}
	


	/*
		开启/关闭管理功能
	*/
	public function setManageSwitch()
	{
		$params = json_decode(file_get_contents('php://input'),true);
		if(isset($params['is_on']))
		{
			$open_id = $_SESSION['WxOpenID'];
			$is_on = $params['is_on'];
			$account_id = $this->getAccountIdByOpenid($open_id);
			if($account_id <= 0)
			{
				show_404();	return;
			}

			$request_array['is_on'] = $params['is_on'];
			$request_array['account_id'] = $account_id;
			$this->load->model('manage/manage_model','',true);
			$result = $this->manage_model->setManageSwitch($request_array);
			
			print_r(json_encode($result));
		}
		else
		{
			show_404();	
		}
	}
	
	//成员view
	public function groupmember() {

		$is_weixin = $this->is_weixin();
		
		if(isset($_SESSION['WxOpenID']))
		{
			$open_id = $_SESSION['WxOpenID'];
			
			$request_ary['open_id'] = $open_id;
			$this->load->model('account/account_model','',true);
			$userinfo_result = $this->account_model->getUserInfo($request_ary);
			if(!isset($userinfo_result['data']['account_id']) || $userinfo_result['data']['account_id'] == "")
			{
				$direct_url = base_url("y/yh");
				Header("Location:".$direct_url);
				return;
			}
			
			$request_ary['account_id'] =$userinfo_result['data']['account_id'];
		
			$data['base_url'] = base_url();
			
			$data['user'] = $userinfo_result['data'];

			$data['open_id'] = "";
			$data['account_id'] = $userinfo_result['data']['account_id'];

			$data['room_url']  = base_url("y/yh");
			$data['share_url'] =  base_url("y/yh");
			$data['share_icon'] = base_url("files/images/game/home.jpg");
			$data['file_url'] = Game_Const::ImageUrl;
			$data['image_url'] = Game_Const::ImageUrl;

			$this->load->model('wechat_model','',true);
			$config_ary = $this->wechat_model->getWxConfig();
			$data['config_ary']=$config_ary;

			$this->load->view("groupmember.php", $data);
		} else {
			$direct_url = base_url("y/yh");
			Header("Location:".$direct_url);
		}
	}


	//邀请函view
	public function invite() {

		$is_weixin = $this->is_weixin();

		if(isset($_SESSION['WxOpenID']) && isset($_GET['code']))
		{
			$open_id = $_SESSION['WxOpenID'];
			
			$request_ary['open_id'] = $open_id;
			$this->load->model('account/account_model','',true);
			$userinfo_result = $this->account_model->getUserInfo($request_ary);
			if(!isset($userinfo_result['data']['account_id']) || $userinfo_result['data']['account_id'] == "")
			{
				$direct_url = base_url("y/invite/".$_GET['code']);
				Header("Location:".$direct_url);
				return;
			}

			$request_ary['account_id'] =$userinfo_result['data']['account_id'];
		
			$data['base_url'] = base_url();
			
			$data['user'] = $userinfo_result['data'];

			$data['open_id'] = "";
			$data['account_id'] = $userinfo_result['data']['account_id'];
			$data['phone'] = $userinfo_result['data']['phone'];

			$data['room_url']  =  base_url("y/invite/".$_GET['code']);
			$data['share_url'] =  base_url("y/invite/".$_GET['code']);
			$data['share_icon'] = base_url("files/images/guild/invitation.png");
			$data['file_url']  = Game_Const::ImageUrl;
			$data['image_url'] = Game_Const::ImageUrl;

			$request_ary['code'] = $_GET['code'];
			$request_ary['account_id'] = $userinfo_result['data']['account_id'];
			$this->load->model('manage/manage_model','',true);
			$result2 = $this->manage_model->getInviteData($request_ary);
			if(!isset($result2['result']) || $result2['result'] != "0")
			{
				log_message('error', "function(invite):getInviteData :"." in file".__FILE__." on Line ".__LINE__);
				show_404();exit;
			}
			
			$this->load->model('wechat_model','',true);
			$config_ary = $this->wechat_model->getWxConfig();
			$data['config_ary']=$config_ary;

            			
			$data['code'] = $_GET['code'];
			$data['invite_nickname'] = $result2['data']['nickname'];
			$data['invite_headimgurl'] = $result2['data']['headimgurl'];
			$data['is_owner'] = $result2['data']['is_owner'];
			$data['invite_status'] = $result2['data']['invite_status'];

			$this->load->view("inviteDetail.php", $data);
		} else {
			$direct_url = base_url("y/invite/".$_GET['code']);
			Header("Location:".$direct_url);
			return;
		}
	}

	
	/*
		加入群组
	*/
	public function joinGroup()
	{
		$params = json_decode(file_get_contents('php://input'),true);
		if(isset($params['user_code']) )
		{
			$open_id = $_SESSION['WxOpenID'];
			$account_id = $this->getAccountIdByOpenid($open_id);
			if($account_id <= 0)
			{
				show_404();	return;
			}

			$request_array['user_code'] = $params['user_code'];
			$request_array['account_id'] = $account_id;
			$this->load->model('manage/manage_model','',true);
			$result = $this->manage_model->joinGroup($request_array);
			
			print_r(json_encode($result));
		}
		else
		{
			show_404();	
		}
	}

    public function changeAdmin()
    {

        $params = file_get_contents('php://input');
        $params = json_decode($params,true);

        if(isset($params['manager_id']) && isset($params['member_id'])&& isset($params['type']))
        {
            $request_array['manager_id'] = $params['manager_id'];
            $request_array['member_id'] = $params['member_id'];
            $request_array['type'] = $params['type'];

            $this->load->model('manage/manage_model','',true);
            $result = $this->manage_model->changeAdmin($request_array);

            $result = json_encode($result);
            print_r($result);
            exit;
        }
        else
        {
            show_404();
        }
    }

    /*
        管理员处理成员
    */
	public function dealMember()
	{
		$params = file_get_contents('php://input');
		$params = json_decode($params,true);
        
		if(isset($params['manager_id']) && isset($params['member_id'])&& isset($params['type']))
		{
			$request_array['manager_id'] = $params['manager_id'];
		    $request_array['member_id'] = $params['member_id'];
		    $request_array['type'] = $params['type'];

		    $this->load->model('manage/manage_model','',true);
		    $result = $this->manage_model->dealMember($request_array);
            
			$result = json_encode($result);
		    print_r($result);
			exit;
		}
		else
		{
			show_404();	
		}
	}
	

	/*
		搜索组内成员
	*/
	public function searchGroupMember()
	{
		$params = file_get_contents('php://input');
		$params = json_decode($params,true);
        
		if(isset($params['page'])&& isset($params['nickname'])&& isset($params['manager_id']))
		{
			$request_array['nickname'] = $params['nickname'];
			$request_array['page'] = $params['page'];
			$request_array['manager_id'] = $params['manager_id'];

			$this->load->model('manage/manage_model','',true);
			$result = $this->manage_model->searchMember($request_array);
			
			$result = json_encode($result);
			print_r($result);
			exit;
		}
		else
		{
			show_404();	
		}
		
	}
	
}
